/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import core.ExceptionMatrix;
import core.Matrix;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alexey Chirkov
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ServerSocket server = null;
        Socket client = null;
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        
        try {
            server = new ServerSocket(8086);
        } catch (IOException ex) {
            System.out.println("Ошибка при запуске сервера");
            return;
        }
        while (true) {
            try {
                client = server.accept();
                oos = new ObjectOutputStream(client.getOutputStream());
                ois = new ObjectInputStream(client.getInputStream());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            Matrix mat1 = new Matrix();
            Matrix mat2 = new Matrix();
            Object res = null;
            try {
                mat1.getMatrix(ois);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            try {
                mat2.getMatrix(ois);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            
            try {
                res = Matrix.execute(mat1, mat2);
            } catch (Exception ex) {
                res = ex.getMessage();
            }
            try {
                oos.writeObject(res);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            
            try {
                ois.close();
                oos.close();
                client.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
